package com.example.chapter05notification;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
// 创建通知渠道
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
        final String zxm = "zxm";
        findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    CharSequence name = "张老师的通知渠道";
                    int importance = NotificationManager.IMPORTANCE_DEFAULT;
                    NotificationChannel channel = new NotificationChannel(zxm, name, importance);
                    notificationManager.createNotificationChannel(channel);  //创建渠道
                }
                NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), zxm)   //CHANNEL_ID:zxm
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("注册完成")
                        .setContentText("张老师的通知渠道");
//                        .setAutoCancel(true);  //设置点击通知后，状态栏自动删除通知。
                Notification notification = builder.build();//调用builder.build()，可获得Notification的对象
                //创建 NotificationManager对象，负责“发出”与“取消”Notification
                notificationManager.notify(1000, notification); //发送通知
                //第1个参数是notification的ID，以后再执行manager.notify(SIMPLE_NOTFICATION_ID, notification);就会更新这个ID的提醒信息
            }
        });
        findViewById(R.id.btnUpdate).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                    CharSequence haha = "haha";
                    NotificationChannel notificationChannel = new NotificationChannel(zxm, haha, NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(notificationChannel);
                }
                NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),zxm)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("提示")
                        .setContentText("我是一只猫，喵！");
                Notification build = builder.build();
                notificationManager.notify(1001,build);
            }
        });
        findViewById(R.id.btnBigText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                    CharSequence haha = "haha";
                    NotificationChannel notificationChannel = new NotificationChannel(zxm, haha, NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(notificationChannel);
                }
                NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(),zxm)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("提示")
                        .setContentText("我是一只猫，喵！")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("这是一个长通知，这是通知的内容这是通知的内容这是……"))
                        ;
                Notification build = builder.build();
                notificationManager.notify(1001,build);
            }
        });
        findViewById(R.id.btnNew).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    CharSequence name = "张老师的通知渠道";
                    int importance = NotificationManager.IMPORTANCE_DEFAULT;
                    NotificationChannel channel = new NotificationChannel(zxm, name, importance);
                    notificationManager.createNotificationChannel(channel);  //创建渠道
                }
                NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), zxm)   //CHANNEL_ID:zxm
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("注册完成New")
                        .setContentText("张老师的通知渠道")

                        .setAutoCancel(true);  //设置点击通知后，状态栏自动删除通知。
                Notification notification = builder.build();//调用builder.build()，可获得Notification的对象
                //创建 NotificationManager对象，负责“发出”与“取消”Notification
                notificationManager.notify(1003, notification); //发送通知
            }
        });
        findViewById(R.id.btnRemove).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notificationManager.cancelAll();
            }
        });
        findViewById(R.id.btnRemoveOne).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notificationManager.cancel(1001);
                //notificationManager.cancel("CC",1001);

            }
        });
    }
}